package com.tomcat.mesos;

import com.google.common.base.Preconditions;
import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 项目配置
 *
 * @author Rayn on 2015/12/21 14:44
 * @email liuwei412552703@163.com
 */
public class Config
{
    private static final Logger LOG = LoggerFactory.getLogger(Config.class);

    public final String zookeeper;
    public final String tomcat;
    public final String port;
    public final String war;
    public final String memory;
    public final String namespace;
    public final Command command;

    public Config(String zookeeper, String tomcat, String port, String war, String memory, String namespace, Command command)
    {
        this.zookeeper = zookeeper;
        this.tomcat = tomcat;
        this.port = port;
        this.war = war;
        this.memory = memory;
        this.namespace = (namespace==null) ? Constants.DEFAULT_NAMESPACE : namespace;
        this.command = command;
    }


    public enum Command
    {
        START,
        STOP,
        STATUS,
        ADD
    }

    /**
     *
     * @return
     */
    public static Options getOptions(){
        /**
         * tomcat home
         */
        Option tomcat = OptionBuilder.withArgName("tomcat")
                .isRequired(false)
                .hasArg()
                .withDescription("Tomcat home")
                .create("tomcat");

        /**
         * tomcat 端口
         */
        Option port = OptionBuilder.withArgName("port")
                .isRequired(false)
                .hasArg()
                .withDescription("tomcat prot.")
                .create( "port" );

        /**
         * 发布的war 包
         */
        Option war = OptionBuilder.withArgName("war")
                .isRequired(false)
                .hasArg()
                .withDescription("WAR 文件")
                .create( "war" );

        Option memory = OptionBuilder.withArgName("memory")
                .isRequired(false)
                .hasArg()
                .withDescription("tomcat 申请资源大小。")
                .create( "memory" );

        Option zookeeper = OptionBuilder.withArgName("zookeeper")
                .isRequired(false)
                .hasArg()
                .withDescription("zookeeper server 例: node1,node2,node3")
                .create( "zookeeper" );

        /**
         * 状态
         */
        OptionGroup optionGroup = new OptionGroup();
        Option start = OptionBuilder.withArgName("start")
                .hasArg(false)
                .withDescription("通过指定的 WAR 来启动一个tomcat")
                .create( "start" );

        Option stop = OptionBuilder.withArgName("stop")
                .hasArg()
                .hasArg(false)
                .withDescription("停止一个指定的容器")
                .create( "stop" );
        Option status = OptionBuilder.withArgName("status")
                .hasArg()
                .hasArg(false)
                .withDescription("查看容器状态。")
                .create( "status" );
        Option add = OptionBuilder.withArgName("add")
                .hasArg()
                .hasArg(false)
                .withDescription("增加一个新的去容器，并启动tomcat")
                .create( "add" );
        optionGroup.addOption(start).addOption(stop).addOption(status).addOption(add).setRequired(true);


        Options options = new Options();
        options.addOptionGroup(optionGroup);
        options.addOption(tomcat).addOption(port).addOption(war).addOption(memory).addOption(zookeeper);

        return options;
    }

    /**
     * 解析命令行参数接口
     * @param args
     * @return
     */
    public static final Config parse(String [] args)
    {
        CommandLineParser parser = new BasicParser();
        CommandLine commandLine = null;
        Command command = null;
        try
        {
            commandLine = parser.parse(getOptions(), args);
        }
        catch (ParseException e)
        {
            LOG.error("\n{}", e.getLocalizedMessage());
            LOG.debug("Can't parse cmd line", e);
        }
        if (commandLine == null)
        {
            printHelp();
            System.exit(-1);
        }

        //解析当前命令
        boolean start = commandLine.hasOption("start");
        boolean stop = commandLine.hasOption("stop");
        boolean status = commandLine.hasOption("status");
        boolean add = commandLine.hasOption("add");

        if (start && stop || stop && status)
        {
            printHelp();
            System.exit(-1);
        }
        if (add) command = Command.ADD;
        if (start) command = Command.START;
        if (stop) command = Command.STOP;
        if (status) command = Command.STATUS;

        if (command == Command.ADD || command == Command.START)
            Preconditions.checkNotNull(commandLine.getOptionValue("war"), "Please specify application with -war");

        return new Config(commandLine.getOptionValue("zookeeper"),
                commandLine.getOptionValue("tomcat"),
                commandLine.getOptionValue("port", "8080"),
                commandLine.getOptionValue("war"),
                commandLine.getOptionValue("memory", "64"),
                commandLine.getOptionValue("ns", "default"),
                command);
    }

    /**
     * 参数传递错误，打印提示信息
     */
    private static void printHelp()
    {

    }
}
